#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>

struct Loaisach
{
 char mls[10];
 char tls[60];	
 };
 struct Sach{
  char ms[10];
  char ts[60];
  char ls[60];
  char tg[60];
  int nxb;
  bool tt;
};
struct SNODE
 {
   	Sach sa;
   	SNODE *link;
   	SNODE *linkcloai;
   };
 struct LNODE
{
	Loaisach ls;
	LNODE *link;
	SNODE *linksach;
};  
 struct SList
 {
  SNODE *first;
  SNODE *last; 	
   };
  struct  LList
{
	LNODE *first;
	LNODE *last;
};
//*****************************************
void RemoveAllLSach (LList &dsl);
void RemoveAllSach (SList &dss);
int AddTailLoaiSach(LList &dsloaisach,LNODE *p);
bool InitLoai(LList &l)
{
	l.first = l.last = NULL;
}
 void InitSach(SList &l)
{
	l.first=l.last= NULL;
}
LNODE* getLNODE(Loaisach loais)
{
	LNODE *p = new LNODE;
	if(p== NULL) return 0;
	p->ls=loais;
	p->link = NULL;
	p->linksach = NULL;
	return p;
}
SNODE* getSNODE(Sach sachget)
{
	SNODE *q = new SNODE;
	if(q == NULL) return 0;
	q->sa = sachget;
	q->link = NULL;
	q->linkcloai = NULL;
	return q;
}
void Hienthisach(SNODE *q)
{
	cout<<"\n----------------------------------------";
	cout<<"\n-       Ma Sach: "<<q->sa.ms;
	cout<<"\n-      Ten Sach: "<<q->sa.ts;
	cout<<"\n-     Loai Sach: "<<q->sa.ls;
	cout<<"\n-       Tac Gia: "<<q->sa.tg;
	cout<<"\n-  Nam Xuat Ban: "<<q->sa.nxb;
	cout<<"\n-    Tinh trang: "<<(q->sa.tt ? "Da muon": "Chua muon.");
}
void HienthiLoaisach(LNODE *p)
{
	cout<<"\nMa: "<<p->ls.mls<<" -  Ten: "<<p->ls.tls;	
	
}
int searchLlist(LList dsls, Loaisach lsachsearch)
{
	LNODE* p= dsls.first;
	while(p != NULL)
	{
		if(strcmp(p->ls.mls,lsachsearch.mls) == 0) return 1;
		p = p->link;
	}
	return 0;
}
LNODE* searchLlist1(LList dsls, char mls[10])
{
	LNODE* p= dsls.first;
	while(p != NULL)
	{
		if(strcmp(p->ls.mls,mls) == 0) return p;
		p = p->link;
	}
	return p;
}
int searchSList(SList dss,Sach sachsrc)
{
	SNODE *q= dss.first;
	while(q !=NULL)
	{
		if(strcmp(q->sa.ms,sachsrc.ms) == 0) return 1;
		q= q->link;
	}
	return 0;
}
LNODE* searchTenloaisach(LList dsls, char tenloaisach[60])
{
		LNODE* p= dsls.first;
	while(p != NULL)
	{
		if(strcmp(p->ls.tls,tenloaisach) == 0) return p;
		p = p->link;
	}
	return p;
}
void searchbyname(SList dss)
{
	char tensach[60];
		cout<<"\nTim kiem theo ten sach: ";
	    gets(tensach);
	    fflush(stdin);	    
	    int dem=0;
	    cout<<"\nTim thay: ";
	    SNODE *q= dss.first;
		while(q !=NULL)
			{
				if(strcmp(q->sa.ts,tensach) == 0) 
				{	
					dem++;
					cout<<"\n"<<dem<<".";
					Hienthisach(q);	
				}
				q= q->link;
			}
	    if(dem == 0) cout<<"\n[!] Khong co cuon \" "<<tensach<<" \" trong danh sach";
}
void searchbyauthor(SList dss)
{
	char tentacgia[60];
		cout<<"\nTim kiem theo tac gia: ";
	    cin>>tentacgia;
	    fflush(stdin);	    
	    int dem=0;
	    cout<<"\nTim thay: ";
	    SNODE *q= dss.first;
		while(q !=NULL)
			{
				if(strcmp(q->sa.tg,tentacgia) ==0) 				
				{	dem++;
					cout<<"\n"<<dem<<".";
					Hienthisach(q);
				}
				q= q->link;
			}
	    if(dem == 0) cout<<"\n[!] Khong co sach cua tac gia "<<tentacgia<<" trong danh sach";
}
void searchbyyear(SList dss)
{
		int namxuatban;
		cout<<"\nTim kiem theo nam: ";
	    cin>>namxuatban;
	    fflush(stdin);	    
	    int dem=0;
	    cout<<"\nTim thay: ";
	    SNODE *q= dss.first;
		while(q !=NULL)
			{
				if(q->sa.nxb == namxuatban) 
				{	dem++;
					cout<<"\n"<<dem<<".";
					Hienthisach(q);
				}
				q= q->link;
			}
	    if(dem == 0) cout<<"\n[!] Khong co sach xuat ban nam "<<namxuatban<<" trong danh sach";
}
int searchbytt(SNODE *q)
{
	if(q->sa.tt == 0) return 0;
	return 1;	
}
int searchbytt1(SList dss)
{
	if(dss.first == NULL){
		cout<<"\nDanh sach rong.";
		return 0;
	} 
	int dem=0;
 	cout<<"\nTim thay: ";
	SNODE *q = dss.first;
	while(q != NULL)
	{
		if(searchbytt(q) == 1)
		{
			dem++;
			cout<<"\n"<<dem<<".";
			Hienthisach(q);
		}
		q = q->link;
	}	
	if(dem == 0) cout<<"\n[!] Khong co sach nao duoc muon";
	return 1;
}
int searchbytt0(SList dss)
{
	if(dss.first == NULL)
	{
		cout<<"\nDanh sach rong.";
		return 0;
	} 
	int dem=0;
 	cout<<"\nTim thay: ";
	SNODE *q = dss.first;
	while(q != NULL)
	{
		if(searchbytt(q) == 0)
		{
			dem++;
			cout<<"\n"<<dem;
			Hienthisach(q);
		}
		q = q->link;
	}	
	if(dem == 0) cout<<"\n[!]Toan bo sach da duoc muon";
	return 1;
}

int searchbytype(LList dsl, SList dss)
{
	char mlsach[10];
	cout<<"/Ma loai sach:";cin>>mlsach;fflush(stdin);
	LNODE * p= searchLlist1(dsl,mlsach);
	if(p == NULL) 
	{
		cout<<"\nKhong co loai sach nay.";
		return 0;
	}
	int dem=0;
 	cout<<"\nTim thay: ";
	SNODE *q= p->linksach;
	while(q != NULL)
	{
			dem++;
			cout<<"\n"<<dem<<".";
			Hienthisach(q);
			q = q->linkcloai;
	}
	if(dem == 0) cout<<"\n[!] Khong co sach loai nay";
}

void RemoveSach(SList &dss,LList &dsl, char tsr[60])
{
	if(dss.first == NULL)
	{
		cout<<"Danh sach rong.";
		return ;
	} 
	SNODE *q= dss.first;
	SNODE *qr;	
	while(q->link != NULL)
	{
		if(strcmp(q->link->sa.ts,tsr) == 0)	
		{
			qr = q->link;
			q->link = qr->link;
		}
		q = q->link;
	}
	if(strcmp(dss.first->sa.ts,tsr) == 0)
	{
		qr = dss.first;
		dss.first = dss.first->link ;
	}
	if(qr == NULL)
	{
		cout<<"\nKhong co cuon sach \""<<tsr<<"\" trong danh sach.";
		return;
	}
	 
	LNODE *p = dsl.first;
	LNODE *pr;
	while(p != NULL)
	{
		if(strcmp(p->ls.tls,qr->sa.ls)==0) 
		{ pr = p;
			break;
		}
		p = p->link;
	}
	q = pr->linksach;
	while(q->linkcloai != NULL)
	{
		if(q->linkcloai == qr) 
		{
			q->linkcloai = qr->linkcloai;
			break;
		}
	}
	if(pr->linksach == qr)
	{
		pr->linksach = qr->linkcloai;
	}
	delete qr;
	cout<<"\nDa xoa: "<<tsr;
	return;
} 
void RemoveLSach(LList &dsl, SList &dss, char tlsr[60])
{
	if(dsl.first == NULL)
	{
		cout<<"\nDanh sach rong.";
		return;
	}
	LNODE *p= dsl.first;
	LNODE *pr;
	while(p->link !=NULL)
	{
		if(strcmp(p->link->ls.tls,tlsr) == 0)
		{
			pr= p->link;
			p->link = pr->link;
			pr->link = NULL;
			break;	
		}
		p = p->link;
	}
	if(strcmp(dsl.first->ls.tls,tlsr) == 0)
	{
		pr = dsl.first;
		dsl.first = dsl.first->link;
	}
	if(pr == NULL)
	{
		cout<<"\n Khong co loai sach nay trong danh sach.";
		return;
	}
	SNODE *q ;
	
		while(pr->linksach != NULL)
		{
			q = dss.first;
				while(q->link !=NULL)
				{
					if(q->link == pr->linksach)
					{
						cout<<"Here";
						SNODE *qr;
						qr= q->link;
						q->link = qr->link;
						pr->linksach = qr->linkcloai;
						delete(qr);
						
					}
					q = q->link;
				}
				if(dss.first == pr->linksach)
				{
					SNODE *qr = dss.first;
					dss.first = dss.first ->link;
					pr->linksach = qr->linkcloai;
					delete(qr);
				}
		}
	delete pr;
	cout<<"\nDa xoa 1 loai sach.";
	return;
}
int AddTailSach(SList &dss,LList &dsl, SNODE *q)
{
		if(q == NULL){
			cout<<"\n~>Them khong thanh cong.";
			return 0;
		}
		if(searchSList(dss,q->sa) == 1)
		{
				cout<<"\n[!] Them khong thanh cong. Da Trung";
				return 0;
		}
		LNODE *p = searchTenloaisach(dsl,q->sa.ls);
		if(p == NULL)
		{
				char i;
				cout<<"\n[!]Loai sach vua nhap khong ton tai.";
				cout<<"\nBan co muon them loai sach nay khong.";
				cout<<"\nTra loi(y/n):";		
				 cin>>i; fflush(stdin);
				 if(i == 'y' || i == 'Y') 
				 {
				 	Loaisach lsachboxung;
				 	strcpy(lsachboxung.tls,q->sa.ls);
		 			cout<<"\n-Ma Loai: ";cin>>lsachboxung.mls;
		 			p = getLNODE(lsachboxung);
		 			if(AddTailLoaiSach(dsl,p) == 0)
					 {	cout<<"\n";
						return 0;
					}
		 			fflush(stdin);
		 		 }
		 		 else{
	 					cout<<"\n";
						return 0;
	 			}
		}
		if(q == NULL)
				{
					cout<<"\n [!] Them khong thanh cong.";
					cout<<"\n";
					return 0;
				}
				if(searchSList(dss,q->sa) == 1)
				{
					cout<<"\n [!] Them khong thanh cong. Trung ma sach";
					cout<<"\n";
					return 0;
				}
				if(dss.last == NULL) 
				{
					dss.first= dss.last = q;
					p->linksach = q;
					
				}
				else{
					dss.last->link = q;
					dss.last = q;
					if(p->linksach == NULL) p->linksach = q;
					else{
						q->linkcloai = p->linksach;
						p->linksach = q;
					}
				}
				q= NULL;
				cout<<"[*] Them sach thanh cong .";
}
int AddTailLoaiSach(LList &dsloaisach,LNODE *p)
{

	if(p == NULL ) 
	{
		cout<<"\n[!]Them khong thanh cong.";
		return 0;
	}
		if(searchLlist(dsloaisach, p->ls)==1) {
			cout<<"\n[!] Them khong thanh cong. Da Trung";
		return 0;
	}
	if(dsloaisach.first == NULL) dsloaisach.first = dsloaisach.last = p;
	else{
		  dsloaisach.last->link = p;
		dsloaisach.last = p;
	}
	cout<<"\n [*]Da them mot loai sach vao danh sach.";
	return 1;	
}
void ThemLoaiSach(LList &dsloaisach)
{
	Loaisach loais;
	cout<<"=======================================\n";
	cout<<"#         THEM LOAI SACH               #\n";
	cout<<"=======================================\n";
	
	
	cout<<"\n-Ma loai sach: "; gets(loais.mls);	
	cout<<"\n-Ten loai sach: "; gets(loais.tls);
	
	LNODE *p = getLNODE(loais);
	AddTailLoaiSach(dsloaisach,p);
}
int TraSach(SList &dss, char mst[10])
{
	if(dss.first == NULL)
	{
		cout<<"\n DANH SACH RONG.";
		return 0;
	}
	SNODE *q= dss.first;
	while(q !=NULL)
	{
		if(strcmp(q->sa.ms,mst) ==0)
		{
			Hienthisach(q);
			if(q->sa.tt == 0){
				cout<<"\n\n~> SACH NAY VAN CHUA DUOC MUON. VUI LONG KIEM TRA LAI.";
				return 0;
			}
			char x;
			cout<<"\n Xac nhan tra[y/n]: ";cin>>x;fflush(stdin);
			if(x == 'Y' || x == 'y'){
				q->sa.tt =0;
				cout<<"\n~>TRA XONG.";
				return 1;
			}
			else 
			{
				cout<<"\n~>BAN  KHONG  MUON TRA SACH NAY . TAM BIET";
				return 0;
			}
		}
		q = q->link;
	}
	cout<<"\nKHONG TIM THAY";
	return 0;
}
int Muonsach(SList &dss, char msm[10])
{
	if(dss.first == NULL)
	{
		cout<<"\n DANH SACH RONG.";
		return 0;
	}
	SNODE *q= dss.first;
	while(q !=NULL)
	{
		if(strcmp(q->sa.ms,msm) ==0)
		{
			Hienthisach(q);
			if(q->sa.tt == 1){
				cout<<"\n\n~> SANH DA DUOC MUON. BAN KHONG THE MUON.";
				return 0;
			}
			char x;
			cout<<"\n~> Xac nhan muon[y/n]: ";cin>>x;fflush(stdin);
			if(x == 'Y' || x == 'y'){
				q->sa.tt =1;
				cout<<"\n~>MUON XONG.";
				return 1;
			}
			else 
			{
				cout<<"\n~>BAN DA KHONG MUON CUON SACH NAY . TAM BIET";
				return 0;
			}
		}
		q = q->link;
	}
	cout<<"\n~>KHONG TIM THAY";
	return 0;
}
int EditBookbyms(SList &dss,LList &dsl, char mse[10])
{
	if(dss.first == NULL)
	{
		cout<<"\n~> DANH SACH RONG.";
		return 0;
	}
	SNODE *q = dss.first;
	while(q !=NULL)
	{
		if(strcmp(q->sa.ms, mse) == 0)
		{
			char mse[10];
	  		char tse[60];
	  		char lse[60];
	  		char tge[60];
  			int nxbe;
			bool tte;
		
			Hienthisach(q);
			cout<<"\n\nSua.";
			cout<<"\n=========================";
			cout<<"\n-Ten Sach: ";
			gets(tse);
			cout<<"\n-Loai Sach: ";
			 gets(lse);
			 LNODE *p =searchTenloaisach(dsl, lse);
			 if( p == NULL)
				{
					char i;
					cout<<"\n[!]Loai sach vua nhap khong ton tai.";
					cout<<"\nBan co muon them loai sach nay khong.";
					cout<<"\nTra loi(y/n):";		
					 cin>>i; fflush(stdin);
					 if(i == 'y' || i == 'Y') 
					 {
					 	Loaisach lsachboxung;
					 	strcpy(lsachboxung.tls,lse);
			 			cout<<"\n-Ma Loai: ";cin>>lsachboxung.mls;
			 			p = getLNODE(lsachboxung);
			 			if(AddTailLoaiSach(dsl,p) == 0) return 0;
			 			fflush(stdin);
			 		 }
			 		 else{
 		 					return 0;
 		 			}
				}
			cout<<"\n-Tac gia: "; gets(tge);
			char n[4];
			cout<<"\n-Nam Xuat Ban:";cin>>n;
			if(isdigit(n[0]))
				{
					if(isdigit(n[1])){
						if(isdigit(n[2])){
							if(isdigit(n[3])){
								nxbe = (n[0]-'0')*1000 + (n[1]-'0')*100+ (n[2]- '0')* 10 + n[3]-'0';				
							}
							else nxbe = (n[0]-'0')*100+ (n[1]- '0')* 10 + n[2]-'0';
						}
						else nxbe = (n[0]- '0')* 10 + n[1]-'0';
					}
					else nxbe =  n[0]-'0';
				}
			else nxbe = 0;
			char i;
			cout<<"\n-Tinh trang[nhap 1-da muon]: ";cin>>i;fflush(stdin);
				if(i =='1') tte= 1;
				else tte =0;
			//*********************
			strcpy(q->sa.ts,tse);
			strcpy(q->sa.ls,lse);
			strcpy(q->sa.tg,tge);
			q->sa.nxb =nxbe;
			q->sa.tt =tte;
			cout<<"\nDA SUA XONG.";
			return 1;
		}
		q = q->link;
	} 
	cout<<"\nKHONG TIM THAY.";
	return 0;
}
int Sortbynamebook(SList &dss)
{
	if(dss.first == NULL) 
	{
		cout<<"\n->DANH SACH RONG.";
		return 0;
	}
	if(dss.first == dss.last)
	{
		cout<<"\n->XONG.";
		return 1;
	}
	SNODE *q= dss.first;
	SNODE *q0;
	SNODE *q1;
	SNODE *qs = dss.last;
	SNODE *tmp;
	while(q->link != NULL)
	{cout<<"1";
		q1= dss.first;
		if(strcmp(q1->sa.ts,q1->link->sa.ts) >0)
		{
			tmp = q1->link;
			q1->link= tmp->link;
			tmp->link = q1;
			dss.first = tmp;
			q0= q1;
		}
		while(q1 != qs)
		{
			cout<<"2";
			if(strcmp(q1->sa.ts,q1->link->sa.ts) > 0)
			{
				tmp = q1->link;
				q1->link = tmp ->link;
				tmp->link = q1;
				q0->link = tmp;
			}
			else q1 = q1->link;
			q0 = q0->link;
		}
		qs = q1;
		q = q->link;
	}
	cout<<"\nXONG.";
	return 1;
}
int SaveLs(LList &dsl)	
{
	FILE *floai= fopen("LoaiSach.dat","wb");
	if(floai ==NULL)
	{
		cout<<"Khong mo duoc file\n";
		getche();
		return 0;
	}
	LNODE *tmp= dsl.first;
	Loaisach lss;
	while(tmp !=NULL)
	{
		lss = tmp->ls;
		fwrite(&lss, sizeof(Loaisach), 1, floai);
		tmp = tmp->link;
	}
	fclose(floai);
	RemoveAllLSach(dsl);
	return 0;
}
int LoadLs(LList &dsl)
{
	FILE *floai = fopen("LoaiSach.dat","rb");
	if(floai == NULL) 
	{
		fputs("Loi file",stderr); return 0;
	}
	
	Loaisach lsload;
	LNODE *p;
	while(!feof(floai))
	{
		if(fread(&lsload, sizeof(lsload), 1,floai)==0)
			break;
		p=getLNODE(lsload);
		AddTailLoaiSach(dsl,p);
	}
	
	fclose(floai);
	return 1;
}
int Savebook(SList &dss)	
{
	FILE *fsach= fopen("Sach.dat","wb");
	if(fsach ==NULL)
	{
		cout<<"Khong mo duoc file\n";
		getche();
		return 0;
	}
	SNODE *tmp= dss.first;
	Sach ss;
	while(tmp != NULL)
	{
		ss = tmp->sa;
		fwrite(&ss, sizeof(Sach), 1, fsach);
		tmp = tmp->link;
	}
	fclose(fsach);
	RemoveAllSach(dss);
	return 0;
}
int Loadbook(SList &dss, LList &dsl)
{
	FILE *fsach = fopen("Sach.dat","rb");
	if(fsach == NULL) 
	{
		fputs("Loi file",stderr); return 0;
	}
	
	Sach sload;
	SNODE *p;
	while(!feof(fsach))
	{
		if(fread(&sload, sizeof(sload), 1,fsach)==0)
			break;
		p=getSNODE(sload);
		AddTailSach(dss,dsl,p);
	}
	
	fclose(fsach);
	return 1;
}
int Xuatdsl(LList dsl)
{
	if(dsl.first == NULL) return 0;
	LNODE *p=dsl.first;
	while(p != NULL)
	{
		HienthiLoaisach(p);
		p =p->link;	
	}
	return 1;
}
int Xuatdss(SList dss)
{
	if(dss.first== NULL) return 0;
	int dem=0;
 	cout<<"\nTim thay: ";
	SNODE *q=dss.first;
	while(q != NULL)
	{
		dem++;
		cout<<"\n"<<dem<<".";
		Hienthisach(q);
		q= q->link;
	}
	return 1;
}
void RemoveAllLSach (LList &dsl)
{
	LNODE *pNode;
	while (dsl.first != NULL)
	{
		pNode = dsl.first;
		dsl.first = dsl.first->link;
		delete pNode;
	}
}
void RemoveAllSach (SList &dss)
{
	SNODE *pNode;
	while (dss.first != NULL)
	{
		pNode = dss.first;
		dss.first = dss.first->link;
		delete pNode;
	}
	dss.last = NULL;
}
 int main()
 {
 
	LList dsl ;
	SList dss;
	InitLoai(dsl);
	InitSach(dss);
	int i,j=0;
	char ix[2];
	SNODE *q;
	bool tieptuc = true;
	while(tieptuc)
	{
		i=-1;
		system("cls");
	cout<<"\t=======================================================\n";
	cout<<"\t#         CAU TRUC DU LIEU VA GIAI THUAT              #\n";
	cout<<"\t#                        *                            #\n";
	cout<<"\t#           DE TAI :QUAN LY THU VIEN                  #\n";
	cout<<"\t=======================================================\n";
	cout<<"Menu:\n";
	cout<<"\t1. Them mot loai sach.\n";
	cout<<"\t2. Them mot cuon sach.\n";
	cout<<"\t3. Tim kiem.\n";
	cout<<"\t4. Luu danh sach sach - loai sach vao file\n";
	cout<<"\t5. Doc danh sach sach - loai sach tu file.\n";
	cout<<"\t6. Xuat danh sach sach.\n";
	cout<<"\t7. Xuat danh sach loai sach.\n";
	cout<<"\t8. Xoa mot cuon sach ra khoi danh sach.\n";
	cout<<"\t9. Xoa mot loai sach ra khoi danh sach.\n";
	cout<<"\t10. Sap xep.\n";
	cout<<"\t11. Sua sach theo ma.\n";
	cout<<"\t12. Muon sach theo ma.\n";
	cout<<"\t13. Tra sach theo ma.\n";
	cout<<"\t14. Thoat.\n";
	cout<<"Chon: ";	
	cin>>ix; fflush(stdin);
	if(isdigit(ix[0]))
		if(isdigit(ix[1])) i= ix[1] -'0' + (ix[0]-'0')*10;
		else i = ix[0] -'0';	
	switch(i)
	{
		case 1: ThemLoaiSach(dsl);
				cout<<"\n";
				system("PAUSE");
				break;
				
		case 2: fflush(stdin);
					char msn[10];
				  	char tsn[60];
				  	char lsn[60];
				  	char tgn[60];
				  	int nxbn;
				 	 bool ttn;
				cout<<"================================\n";
				cout<<"#         THEM  SACH           #\n";
				cout<<"================================\n";
				
				cout<<"\n-Ma Sach: "; cin>>msn;
					fflush(stdin);
				cout<<"-Ten Sach: "; gets(tsn);
				cout<<"-Loai Sach: "; gets(lsn);
				cout<<"\n-Tac gia: "; gets(tgn);
				char n[4];
				cout<<"\n-Nam Xuat Ban:";cin>>n;fflush(stdin);
				if(isdigit(n[0]))
					{
						if(isdigit(n[1])){
							if(isdigit(n[2])){
								if(isdigit(n[3])){
									nxbn = (n[0]-'0')*1000 + (n[1]-'0')*100+ (n[2]- '0')* 10 + n[3]-'0';				
								}
								else nxbn = (n[0]-'0')*100+ (n[1]- '0')* 10 + n[2]-'0';
							}
							else nxbn = (n[0]- '0')* 10 + n[1]-'0';
						}
						else nxbn =  n[0]-'0';
					}
				else nxbn = 0;
				
				cout<<"\n-Tinh trang[nhap 1-da muon]: ";cin>>ix;fflush(stdin);
				if(ix[0] =='1') ttn= 1;
				else ttn =0;		
				Sach sachn;
				strcpy(sachn.ms,msn);
				strcpy(sachn.ts,tsn);
				strcpy(sachn.ls,lsn);
				strcpy(sachn.tg,tgn);
				sachn.nxb = nxbn;
				sachn.tt = ttn;				
				q = getSNODE(sachn);
				AddTailSach(dss,dsl,q);
				cout<<"\n";
				system("PAUSE");
				break;
		case 3:	
				// TIm kiem
				j=-1;
				cout<<"================================\n";
				cout<<"#         TIM  KIEM            #\n";
				cout<<"================================\n";		
				cout<<"MENU:\n";
				cout<<"\t\n1.Tim sach theo ten.";
				cout<<"\t\n2.Tim sach theo tac gia.";
				cout<<"\t\n3.Tim sach theo nam xuat ban.";
				cout<<"\t\n4.Tim sach da duoc muon.";
				cout<<"\t\n5.Tim sach chua muon.";
				cout<<"\t\n6.Tim sach theo loai sach.";
				cout<<"\t\n7.Thoat khoi tim kiem.";
				cout<<"\nChon: "; cin>>ix; fflush(stdin);
				if(isdigit(ix[0]))
					if(isdigit(ix[1])) j= ix[1] -'0' + (ix[0]-'0')*10;
					else j = ix[0] -'0';
				switch(j)
				{
					case 1:	// TIm kiem theo ten sach
							searchbyname(dss);
							cout<<"\n";
							system("PAUSE");
							break;
					case 2: // tim kiem theo ten tac gia
							searchbyauthor(dss);
							cout<<"\n";
							system("PAUSE");
							break;
					case 3: // tim kiem theo nam
							searchbyyear(dss);
							cout<<"\n";
							system("PAUSE");
							break;		
					case 4://tim sach da muon
							searchbytt1(dss);
							cout<<"\n";
							system("PAUSE");
							break;	
					case 5:	//tim sach chua muon
							searchbytt0(dss);
							cout<<"\n";
							system("PAUSE");
							break;
					case 6:// tim sach theo loai sach
							searchbytype(dsl,dss);	
						 	cout<<"\n";
							system("PAUSE");
							break;
					case 7: break;
				}	
				break;
		case 4:// Luu danh sach loai danh sach vaO file nhi phan
				// luu danh sach loai
				char ng;
				cout<<"Luu sach va loai sach vao file (y/n): ";cin>>ng;fflush(stdin);
				if(ng == 'y' || ng == 'Y'){
					SaveLs(dsl);
					Savebook(dss);
					cout<<"Luu xong.";
					cout<<"\n";
					system("PAUSE");
					break;
				}
					cout<<"\nHUY.";
					cout<<"\n";
					system("PAUSE");
					break;					
		case 5: //Doc file 
				// doc LoaiSach.dat [!]chua doc duoc
				LoadLs(dsl);
				Loadbook(dss,dsl);
				cout<<"\nDoc xong";
				cout<<"\n";
				system("PAUSE");
				break;	
		case 6: //Xuat danh sach sach.
				cout<<"\n======================";
				cout<<"\nDanh sach  sach:";
				Xuatdss(dss);
				cout<<"\n";
				system("PAUSE");
				break;
				
		case 7: // xuat danh sach loai sach.
				cout<<"\n======================";
				cout<<"\nDanh sach loai sach:";
				Xuatdsl(dsl);
				cout<<"\n";
				system("PAUSE");
				break;
		case 8: // Xoa mot cuon sach ra khoi danh sach.
				cout<<"\n===========";
				cout<<"\n*Xoa Sach *";
				cout<<"\n===========";
				char tsachr[60];
				cout<<"\nNhap ten sach: "; gets(tsachr);
				RemoveSach(dss,dsl,tsachr);
				cout<<"\n";
				system("PAUSE");
				break;
		case 9://Xoa 1 loai sach ra khoi danh sach
				cout<<"\n================";
				cout<<"\n*Xoa Loai Sach *";
				cout<<"\n================";
				char tlsr[60];	
				cout<<"\nNhap ten loai sach: "; gets(tlsr);
				RemoveLSach(dsl,dss,tlsr);
				cout<<"\n";
				system("PAUSE");
				break;
		case 10:// Sap xep
				j=-1;
				cout<<"\n==============================";
				cout<<"\n#          SAP XEP           #";
				cout<<"\n==============================";
				cout<<"\t\n1.Sap xep danh sach sach theo ten.";
				cout<<"\t\n2.Sap xep danh sach loai theo ten.";
				cout<<"\t\n3.Sap xep danh sach sach theo loai sach.";
				cout<<"\t\n4.Sap xep danh sach loai theo ma sach.";
				cout<<"\t\n5.Sap xep danh sach sach theo nam xuat ban.";
				cout<<"\t\n6.Thoat khoi sap xep.";
				cout<<"\nChon: ";
				cin>>ix; fflush(stdin);
				if(isdigit(ix[0]))
						if(isdigit(ix[1])) j= ix[1] -'0' + (ix[0]-'0')*10;
						else j = ix[0] -'0';
				switch(j)
				{
					case 1:// Sap xep danh sach sach theo ten;
							Sortbynamebook(dss);
							cout<<"\n";
							system("PAUSE");
							break;
					case 6: break;
				}
				break;
		case 11: //sua sach theo ms
				cout<<"\n===========================";
				cout<<"\n#       SUA SACH          #";
				cout<<"\n===========================";\
				char mse[10];
				cout<<"\n Ma sach: "; cin>> mse; fflush(stdin);
				EditBookbyms(dss,dsl,mse);
				cout<<"\n";
				system("PAUSE");
				break;
		case 12:// Muon sach theo ma
				cout<<"\n-----------------------";
				cout<<"\n|      MUON SACH      |";
				cout<<"\n-----------------------";
				char msm[10];
				cout<<"\nMa sach muon muon: "; cin>>msm;
				fflush(stdin);
				Muonsach(dss, msm);
				cout<<"\n";
				system("PAUSE");
				break;
		case 13: // tra sach theo ma
				cout<<"\n-----------------------";
				cout<<"\n|      TRA SACH        |";
				cout<<"\n-----------------------";
				char mst[10];
				cout<<"\nMa sach muon tra: "; cin>>mst;
				fflush(stdin);
				TraSach(dss, mst);
				cout<<"\n";
				system("PAUSE");
				break;
		case 14:// thoat khoi chuong trinh
				tieptuc = false;
				cout<<"\n";				
				break;
	}
	}
	system("cls");
	cout<<"\n\n\n\t\t==================================================";
	cout<<"\n\t\t||\t\tCHUONG TRINH KET THUC\t\t||\n";
	cout<<"\t\t==================================================\n\n\n";	
	cout<<"	\n\n\n\n\t\t     GOOD BYE  __  HAVE A GOOD DAY !!!";
	cout<<"\n\n\n\n\n\n\n\n\n";
	return 1;
	}